Skip to content

fix(db): fully reconcile retrieval_owner_matches drift + harden PUBLIC-execute on retrieval helpers#403

Merged
BigSimmo merged 1 commit into
mainfrom
claude/reconcile-retrieval-owner-search-path
Jul 8, 2026
Merged

fix(db): fully reconcile retrieval_owner_matches drift + harden PUBLIC-execute on retrieval helpers#403
BigSimmo merged 1 commit into
mainfrom
claude/reconcile-retrieval-owner-search-path

Conversation

@BigSimmo

@BigSimmo BigSimmo commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Fully reconciles the retrieval_owner_matches schema/live drift and closes the related PUBLIC-execute drift on search_document_chunks. Two logically-grouped commits.

1. search_path reconciliation (commit f7e3d91)

schema.sql declared set search_path = public, pg_temp for retrieval_owner_matches, while live and both migrations (20260705210000, 20260708150000) use public, pg_catalog. schema.sql was the sole outlier → fresh-replay def_hash diverged from live. Verified read-only against live (sjrfecxgysukkwxsowpy) via schema_drift_snapshot(): body byte-identical, def_hash 1d88b539…. Behaviour-identical (pg_catalog is the safer choice for an immutable function).

  • supabase/schema.sqlpg_temppg_catalog (this fn only).
  • supabase/drift-manifest.jsondef_hash → live value; schema_sha256 recomputed.

2. PUBLIC-execute hardening — applied to live (commit ba45ed2)

Both retrieval_owner_matches and search_document_chunks retained the default PUBLIC EXECUTE grant on live, drifting from schema.sql's declared blanket posture (revoke execute on all functions in schema public from public, anon, authenticated + grant to service_role).

Safety verified before applying — both are server-only, so revoking PUBLIC is behaviour-neutral:

  • search_document_chunks is called via the service-role admin client (src/app/api/documents/[id]/search/route.ts), which keeps execute.
  • retrieval_owner_matches is only invoked nested inside retrieval RPCs that run as service_role.
  • RLS still applies to these security-invoker functions regardless.

Applied to live via Supabase MCP (recorded version 20260708150150); verified read-only that both ACLs are now {postgres,service_role} (PUBLIC removed), matching the manifest. Security advisor: no lints.

  • New migration supabase/migrations/20260708150150_harden_retrieval_public_execute.sql — idempotent; no-op on schema.sql-provisioned/preview envs. Version matches live's recorded migration history (no version-stamp drift).
  • supabase/drift-allowlist.json — removed both now-resolved entries.
  • docs/database-drift-detection.md — recorded the reconciliation.

Scope note

Three other functions carry the same PUBLIC-execute drift (document_summary_text(uuid), detect_legacy_ivfflat_indexes(), set_document_embedding_field_content_hash() — the last a trigger fn whose ACL is cosmetic). Left out of this PR: they weren't in scope and their nested-call safety wasn't individually verified. Same one-line pattern applies if you want a follow-up.

Verification

  • tests/supabase-schema.test.ts42/42 pass.
  • prettier --check on all edited files — pass.
  • Live cross-checks via schema_drift_snapshot() / pg_proc.proacl (read-only), security advisor clean.
  • Not run: check:drift (needs live service-role key in env), drift:manifest (Docker OOM here) — both covered by the read-only live verification. Manifest was hand-edited (documented, same as fix(db): remove phantom ingestion_job_stages.job_id FK to match live (R24e) #389).

Clinical Governance Preflight

DB schema/grants only; no clinical-output behaviour change. Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy). The one live change is a privilege revoke (tightening), applied via MCP and verified; service-role access remains server-only. No patient-data workflow touched.

🤖 Generated with Claude Code

…sql -> pg_catalog)

schema.sql declared `set search_path = public, pg_temp` for
retrieval_owner_matches while both live and the migrations
(20260705210000, 20260708150000) use `public, pg_catalog`. schema.sql was
the sole outlier, so a fresh schema.sql replay diverged from live on this
function's def_hash — noise the drift detector would flag.

Verified read-only against live (project sjrfecxgysukkwxsowpy) via
schema_drift_snapshot(): live body is byte-identical and uses
`public, pg_catalog` (def_hash 1d88b539bded5aa40393125f672b8cab).

Changes (behaviour-identical — the body references nothing schema-qualified;
pg_catalog is the safer choice for an immutable function):
- schema.sql: pg_temp -> pg_catalog for retrieval_owner_matches.
- drift-manifest.json: def_hash -> live's value; schema_sha256 recomputed
  (normalizedSchemaSha256 of the edited schema.sql).
- drift-allowlist.json: the entry mislabelled this tiny boolean helper as a
  "live-ahead richer-body" RPC (copy-paste from match_document_chunks et al.).
  Narrowed to the real remaining drift: the PUBLIC-execute ACL, same posture
  as search_document_chunks (owner-managed hardening; revoke on live to close).
- docs/database-drift-detection.md: removed retrieval_owner_matches from the
  "forward-codify live bodies" backlog group; noted the partial reconciliation.

Manifest was hand-edited (Docker drift:manifest replay OOMs in this env, per
the deploy-status notes); a reviewer can run `npm run drift:manifest` to
confirm the snapshot def_hash + schema_sha256 match a fresh replay.

tests/supabase-schema.test.ts: 42/42 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@supabase

supabase Bot commented Jul 8, 2026

Copy link
Copy Markdown

Updates to Preview Branch (claude/reconcile-retrieval-owner-search-path) ↗︎

Deployments Status Updated
Database Wed, 08 Jul 2026 14:55:02 UTC
Services Wed, 08 Jul 2026 14:55:02 UTC
APIs Wed, 08 Jul 2026 14:55:02 UTC

Tasks are run on every commit but only new migration files are pushed.
Close and reopen this PR if you want to apply changes from existing seed or migration files.

Tasks Status Updated
Configurations Wed, 08 Jul 2026 14:55:13 UTC
Migrations Wed, 08 Jul 2026 14:56:07 UTC
Seeding Wed, 08 Jul 2026 14:56:11 UTC
Edge Functions Wed, 08 Jul 2026 14:56:12 UTC

View logs for this Workflow Run ↗︎.
Learn more about Supabase for Git ↗︎.

@BigSimmo
BigSimmo enabled auto-merge (squash) July 8, 2026 14:58
@BigSimmo
BigSimmo merged commit 224143b into main Jul 8, 2026
6 checks passed
@BigSimmo BigSimmo changed the title fix(db): reconcile retrieval_owner_matches search_path drift (schema.sql → pg_catalog) fix(db): fully reconcile retrieval_owner_matches drift + harden PUBLIC-execute on retrieval helpers Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant